//项目路径
var project = 'http://localhost/website';

$(function () {
   //获取地址栏参数
    var cid = getParamter("cid");
    var csid = getParamter("csid");
    var pageNow = getParamter("pageNow");
    if (pageNow == "") {
        pageNow = 1;
    }
    if (cid != "") {//查询一级类目下新闻
        findNewsByCid(cid, pageNow);
    }
    if (csid != "") {//查询二级类目下新闻
        findNewsByCsid(csid, pageNow);
    }
    if(cid =="" && csid ==""){
        location.href =  project + '/index.html'
        return
    }

   //类目
    listCategoryForProduct(cid, csid);

})


//根据一级类目查询产品
function findNewsByCid(cid, pageNow) {
    $.ajax({
        url: project + '/product/findProductsByCid',
        dataType: 'json',
        type: 'get',
        data: {
            pageNow: pageNow,
            cid: cid
        },
        success: function (data) {
           //*******宣传语*********//
            var data = eval(data);
           // 宣传语1
            var publicityOne = $($(".up-cont").children("span").get(0))
            var publicityOne_val = data.categoryExt.publicityOne
            publicityOne.text(publicityOne_val)
           // 宣传语2
            var publicityTwo = $($(".up-cont").children("span").get(1))
            var publicityTwo_val = data.categoryExt.publicityTwo
            publicityTwo.text(publicityTwo_val)
            $(".img-responsive").attr("src","http://localhost:8080/website_manager"+data.categoryExt.image);
           //*******二级类目显示*********//








           ////***********************产品列表******************************
            $.each(data.list, function (key, val) {
                var pid = val["id"];
                var proName = val["proName"];
                var proInfo = val["proInfo"];
                var proPicture = val["proPicture"];
                var createTime = val["createTime"];
                var proHref = val["proHref"];
                var proKind = val["proKind"];
                var csid = val["csid"];
                var temp = `<li>
	<a class="img-box" href="`+project +`/productinfo.html?pid=`+pid+`" target="_blank">
		<img class="bgimg" width="100%" src="img/H5_kuang.png" alt="">
		<img class="up-pic" src="http://localhost:8080/website_manager/images`+proPicture+`" alt="">
		<span class="up-erwei">
			<span class="bg"></span>
	   </span>
	</a>
	<div class="cont-box">
			<p class="stit"><a href="`+project +`/productinfo.html?pid=`+pid+`" target="_blank">`+proName+`</a></p>
			<div class="lab-font">
				 <span>类型：`+data.categoryExt.cname+`</span>
			</div>
			<div class="bot-link f-cb">
				<a class="fl link-detail" href="`+project +`/productinfo.html?pid=`+pid+`" target="_blank">Details</a>
				<a class="fr point" href="`+project +`/productinfo.html?pid=`+pid+`" target="_blank"></a>
			</div>
	</div>
</li>
`
                $(".mt30").append(temp);
            })


           //********* 分页 ****************/
           //*******声明变量*********//
           //翻页按钮
            var numList = data.navigatepageNums //[1~5]
            var pages = data.pages//总页数
            var pageNow = data.pageNum//当前页
            var prePage = data.prePage//上一页
            var nextPage = data.nextPage// 下一页
            var hasPreviousPage = data.hasPreviousPage//是否有上一页
            var hasNextPage = data.hasNextPage// 是否有下一页

            var str = `<span> 共`+pages+`页 页次:`+pageNow+`/`+pages+`页</span>\n`

           //上一页
            if (hasPreviousPage == true) {
                str = str + " <a href='/website/products.html?cid=" + cid + "&pageNow=1'>首页</a>\n" +
                    "                <a href='/website/products.html?cid=" + cid + "&pageNow=" + (pageNow - 1) + "'>上一页</a>\n"
            }
           //页码
            for (var i = 0; i < numList.length; i++) {
                if (numList[i] == pageNow) {
                    str = str + "<span><font color='red'>" + pageNow + "</font></span>"
                } else {
                    str = str + "<a href='/website/products.html?cid=" + cid + "&pageNow=" + numList[i] + "'>" + numList[i] + "</a>\n"
                }
            }
           //下一页
            if (hasNextPage == true) {
                str = str + "<a href='/website/products.html?cid=" + cid + "&pageNow=" + (pageNow + 1) + "'>下一页</a>\n" +
                    "<a href='/website/products.html?cid=" + cid + "&pageNow=" + pages + "'>尾页</a>"
            }
           //准备好的数据加入其中
            $(".last").append(str)
        }

    })
}

//根据二级类目查询产品
function findNewsByCsid(csid, pageNow) {
    $.ajax({
        url: project + '/product/findProductsByCsid',
        dataType: 'json',
        type: 'get',
        data: {
            pageNow: pageNow,
            csid: csid
        },
        success: function (data) {
           //*******宣传语*********//
           var data = eval(data);
           // 宣传语1
            var publicityOne = $($(".up-cont").children("span").get(0))
            var publicityOne_val = data.categorySecond.publicityOne
            publicityOne.text(publicityOne_val)
           // 宣传语2
            var publicityTwo = $($(".up-cont").children("span").get(1))
            var publicityTwo_val = data.categorySecond.publicityTwo
            publicityTwo.text(publicityTwo_val)
            //banner图
            $(".img-responsive").attr("src","http://localhost:8080/website_manager"+data.categorySecond.image);
           ////***********************产品列表******************************
            $.each(data.list, function (key, val) {
                var pid = val["id"];
                var proName = val["proName"];
                var proInfo = val["proInfo"];
                var proPicture = val["proPicture"];
                var createTime = val["createTime"];
                var proHref = val["proHref"];
                var proKind = val["proKind"];
                var csid = val["csid"];

                var temp = `<li>
	<a class="img-box" href="`+project +`/productinfo.html?pid=`+pid+`" target="_blank">
		<img class="bgimg" width="100%" src="img/H5_kuang.png" alt="">
		<img class="up-pic" src="http://localhost:8080/website_manager/images`+proPicture+`" alt="">
		<span class="up-erwei">
			<span class="bg"></span>
	   </span>
	</a>
	<div class="cont-box">
			<p class="stit"><a href="`+project +`/productinfo.html?pid=`+pid+`" target="_blank">`+proName+`</a></p>
			<div class="lab-font">
				 <span>类型：`+data.categorySecond.csname+`</span>
			</div>
			<div class="bot-link f-cb">
				<a class="fl link-detail" href="`+project +`/productinfo.html?pid=`+pid+`" target="_blank">Details</a>
				<a class="fr point" href="`+project +`/productinfo.html?pid=`+pid+`" target="_blank"></a>
			</div>
	</div>
</li>
`
                $(".mt30").append(temp);
            })


           //********* 分页 ****************/
           //*******声明变量*********//
           //翻页按钮
            var numList = data.navigatepageNums //[1~5]
            var pages = data.pages//总页数
            var pageNow = data.pageNum//当前页
            var prePage = data.prePage//上一页
            var nextPage = data.nextPage// 下一页
            var hasPreviousPage = data.hasPreviousPage//是否有上一页
            var hasNextPage = data.hasNextPage// 是否有下一页

            var str = `<span> 共`+pages+`页 页次:`+pageNow+`/`+pages+`页</span>\n`

           //上一页
            if (hasPreviousPage == true) {
                str = str + " <a href='/website/products.html?csid=" + csid + "&pageNow=1'>首页</a>\n" +
                    "                <a href='/website/products.html?csid=" + csid + "&pageNow=" + (pageNow - 1) + "'>上一页</a>\n"
            }
           //页码
            for (var i = 0; i < numList.length; i++) {
                if (numList[i] == pageNow) {
                    str = str + "<span><font color='red'>" + pageNow + "</font></span>"
                } else {
                    str = str + "<a href='/website/products.html?csid=" + csid + "&pageNow=" + numList[i] + "'>" + numList[i] + "</a>\n"
                }
            }
           //下一页
            if (hasNextPage == true) {
                str = str + "<a href='/website/products.html?csid=" + csid + "&pageNow=" + (pageNow + 1) + "'>下一页</a>\n" +
                    "<a href='/website/products.html?csid=" + csid + "&pageNow=" + pages + "'>尾页</a>"
            }
           //准备好的数据加入其中
            $(".last").append(str)
        }

    })
}

